/*
 * Copyright (c) 2011, INRIA
 * All rights reserved.
 */
package fr.inria.wimmics.semanticweb.filter.sparql;

import fr.inria.wimmics.semanticweb.filter.instance.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.ActionListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import org.gephi.filters.spi.FilterProperty;

/**
 *
 * @author Erwan Demairy <Erwan.Demairy@inria.fr>
 */
public class SPARQLFilterPanel extends javax.swing.JPanel implements ActionListener {

    private final SPARQLFilter filter;

    public SPARQLFilterPanel(SPARQLFilter filter) {
        this.filter = filter;
        initComponents();
        updateButton.addActionListener(this);
        String request = filter.getSelectRequest();
        if ((request != null) && !request.isEmpty()) {
            sparqlRequestEditor.setQueryText(request);
        } else {
            updateFilterValue();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sparqlRequestEditor = new fr.inria.acacia.corese.gui.query.SparqlQueryEditor();
        updateButton = new javax.swing.JButton();

        sparqlRequestEditor.setMinimumSize(new java.awt.Dimension(431, 253));
        sparqlRequestEditor.setName(""); // NOI18N
        sparqlRequestEditor.setQueryText(org.openide.util.NbBundle.getMessage(SPARQLFilterPanel.class, "SPARQLFilterPanel.queryText")); // NOI18N

        updateButton.setText(org.openide.util.NbBundle.getMessage(SPARQLFilterPanel.class, "SPARQLFilterPanel.updateButton.text")); // NOI18N
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sparqlRequestEditor, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)
                    .addComponent(updateButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(sparqlRequestEditor, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(updateButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_updateButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private fr.inria.acacia.corese.gui.query.SparqlQueryEditor sparqlRequestEditor;
    private javax.swing.JButton updateButton;
    // End of variables declaration//GEN-END:variables

    private void updateFilterValue() {
        FilterProperty typeToFilter = filter.getProperties()[0];
        typeToFilter.setValue(sparqlRequestEditor.getQueryText());

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        updateFilterValue();
    }
}
